package com.example.demo.mvvm.ui.vm.fragment;

import android.content.Intent;
import android.databinding.ObservableField;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.Toast;

import com.example.demo.mvvm.BR;
import com.example.demo.mvvm.R;
import com.example.demo.mvvm.bean.ArticleBean;
import com.example.demo.mvvm.bean.BannerBean;
import com.example.demo.mvvm.bean.ListDto;
import com.example.demo.mvvm.databinding.FragmentMainHomeBinding;
import com.example.demo.mvvm.net.BaseObserver;
import com.example.demo.mvvm.net.NetRequest;
import com.example.demo.mvvm.ui.activity.ChildrenActivity;
import com.example.demo.mvvm.ui.activity.LoginActivity;
import com.example.demo.mvvm.ui.activity.WebActivity;
import com.example.demo.mvvm.ui.base.BaseRecyclerViewVM;
import com.example.demo.mvvm.utils.ActivityUtils;
import com.example.demo.mvvm.utils.AppCacheSharedPreferences;
import com.example.demo.mvvm.utils.OnItemClickListener;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.youth.banner.listener.OnBannerListener;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * Created by 80711 on 2018/5/16.
 */

public class HomeFragmentVm extends BaseRecyclerViewVM<ArticleBean> {

    public ObservableField<List<BannerBean>> bannerList = new ObservableField<>();

    public ObservableField<LinearLayoutManager> manage = new ObservableField<>();

    public ObservableField<OnBannerListener> lis = new ObservableField<>();

    public final ItemBinding<ArticleBean> itemBinding = ItemBinding.<ArticleBean>of(BR.item, R.layout.item_home_article).bindExtra(BR.listener, new OnItemClickListener<ArticleBean>() {
        @Override
        public void onItemClick(int viewId , ArticleBean data) {
            switch (viewId){
                case R.id.root:
                    ActivityUtils.push(WebActivity.class , new Intent().putExtra("activityUrl" ,data.getLink() ));
                    break;
                case R.id.chapter_tv:
                    ActivityUtils.push(ActivityUtils.peek() , ChildrenActivity.class , new Intent().putExtra("articleBean" , data));
                    break;
                case R.id.homeItemLike:
                    if(AppCacheSharedPreferences.getCacheBoolean("IS_LOGIN" , false)){
                        request_zan(data);
                    }else{
                        ActivityUtils.push(ActivityUtils.peek() , LoginActivity.class );
                    }
                    break;
                    //Toast.makeText( ActivityUtils.peek() , data.getChapterName(), Toast.LENGTH_SHORT).show();
            }
        }
    });



    public int pageNum = 0;
    private FragmentMainHomeBinding binding;

    public HomeFragmentVm(final FragmentMainHomeBinding binding) {
        this.binding = binding;
        manage.set(new LinearLayoutManager(binding.getRoot().getContext()));
        listener.set(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                request_data(refreshlayout);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNum = 0;
                request_data(refreshlayout);
                request_banner(refreshlayout);
            }
        });
        lis.set(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                ActivityUtils.push(WebActivity.class , new Intent().putExtra("activityUrl" ,bannerList.get().get(position).getUrl() ));
            }
        });
    }

    private void request_banner(RefreshLayout refreshlayout) {
        NetRequest.getHomeBannerInfo(new BaseObserver<List<BannerBean>>(binding.getRoot().getContext() ,refreshlayout , false ) {
            @Override
            public void onSuccess(List<BannerBean> bannerBeans, RefreshLayout refreshLayout) {
                bannerList.set(bannerBeans);
            }

            @Override
            public void onFailure(int code, String message, RefreshLayout refreshLayout) {

            }
        });
    }

    private void request_data(RefreshLayout refreshlayout) {
        NetRequest.getHomeArticle(pageNum, new BaseObserver<ListDto<ArticleBean>>(refreshlayout.getLayout().getContext(), refreshlayout, false) {
            @Override
            public void onSuccess(ListDto<ArticleBean> articleBeanListDto, RefreshLayout refreshLayout) {
                if (pageNum == 0) {
                    items.clear();
                }
                pageNum += 1;
                items.addAll(articleBeanListDto.getDatas());
                refreshLayout.finishRefresh(true);
                refreshLayout.finishLoadmore(true);
            }

            @Override
            public void onFailure(int code, String message, RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(false);
                refreshLayout.finishLoadmore(false);
            }
        });
    }

    private void request_zan(final ArticleBean bean){
        //flag -> true 表示 需要添加 ,false则为取消收藏
        boolean flag = !bean.isCollect();
        if(flag){
            NetRequest.addCollect(bean.getId(), new BaseObserver(ActivityUtils.peek() , false) {
                @Override
                public void onSuccess(Object o, RefreshLayout refreshLayout) {
                    bean.setCollect(true);
                    Toast.makeText(ActivityUtils.peek(), "收藏成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(int code, String message, RefreshLayout refreshLayout) {

                }

            });
        }else{
            NetRequest.removeCollect(bean.getId(), bean.getOrginId() ,new BaseObserver(ActivityUtils.peek() , false) {
                @Override
                public void onSuccess(Object o, RefreshLayout refreshLayout) {
                    bean.setCollect(false);
                    Toast.makeText(ActivityUtils.peek(), "取消收藏成功" , Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(int code, String message, RefreshLayout refreshLayout) {

                }

            });
        }
    }


}
